package org.gdstash.item;

import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public abstract class GDAbstractContainer implements GDItemContainer {
    protected List<GDItem> items;
    protected List<GDItem> removedItems;
    protected boolean changed;
    protected int containerType;
    protected int width;
    protected int height;

    public GDAbstractContainer(final int containerType) {
        this.containerType = containerType;
        this.items = new LinkedList<GDItem>();
        this.removedItems = new LinkedList<GDItem>();
        this.changed = false;
        this.width = -1;
        this.height = -1;
    }

    @Override
    public List<GDItem> getItemList() {
        final List<GDItem> list = new LinkedList<GDItem>();
        for (final GDItem item : this.items) {
            list.add(item);
        }
        return list;
    }

    @Override
    public List<GDItem> getRemovedItemList() {
        final List<GDItem> list = new LinkedList<GDItem>();
        for (final GDItem item : this.removedItems) {
            list.add(item);
        }
        return list;
    }

    @Override
    public boolean addItem(final GDItem item) {
        item.setContainerType(this.containerType);
        this.items.add(item);
        return this.changed = true;
    }

    @Override
    public boolean removeItem(final GDItem item) {
        boolean found = false;
        if (this.items != null) {
            final Iterator<GDItem> iter = this.items.iterator();
            while (iter.hasNext()) {
                final GDItem ti = iter.next();
                if (ti == item) {
                    iter.remove();
                    found = true;
                    this.changed = true;
                    break;
                }
            }
        }
        return found;
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public int getContainerHeight() {
        if (this.height > 0) {
            return this.height;
        }
        int h = 0;
        switch (this.containerType) {
            case 1: {
                h = 16;
                break;
            }
            case 3: {
                h = 18;
                break;
            }
            case 4: {
                h = 8;
                break;
            }
            case 5: {
                h = 8;
                break;
            }
            default: {
                h = 16;
                break;
            }
        }
        return h;
    }

    @Override
    public int getContainerWidth() {
        if (this.width > 0) {
            return this.width;
        }
        int w = 0;
        switch (this.containerType) {
            case 1: {
                w = 8;
                break;
            }
            case 3: {
                w = 10;
                break;
            }
            case 4: {
                w = 12;
                break;
            }
            case 5: {
                w = 8;
                break;
            }
            default: {
                w = 8;
                break;
            }
        }
        return w;
    }

    @Override
    public int getContainerType() {
        return this.containerType;
    }

    public void setContainerType(final int containerType) {
        this.containerType = containerType;
    }

    @Override
    public void refresh() {
        final List<GDItem> temp = new LinkedList<GDItem>();
        Iterator<GDItem> iter = this.items.iterator();
        while (iter.hasNext()) {
            final GDItem item = iter.next();
            item.refresh();
            if (item.hasErrors()) {
                try {
                    iter.remove();
                } catch (UnsupportedOperationException ex) {
                }
                temp.add(item);
            }
        }
        iter = this.removedItems.iterator();
        while (iter.hasNext()) {
            final GDItem item = iter.next();
            item.refresh();
            if (!item.hasErrors()) {
                try {
                    iter.remove();
                    this.items.add(item);
                } catch (UnsupportedOperationException ex2) {
                }
            }
        }
        if (!temp.isEmpty()) {
            iter = temp.iterator();
            while (iter.hasNext()) {
                final GDItem item = iter.next();
                this.removedItems.add(item);
            }
        }
        if (!this.removedItems.isEmpty()) {
            GDMsgLogger.addWarning(GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_STASH_ITEMS_REMOVED"));
        }
    }
}
